const jwt = require("jsonwebtoken");
const Service = require("../../services");
const { USER_TOKEN_SECRET_KEY, TOKEN_ISSUE_KEY } = require("../../sys_config");

module.exports = {
  name: "user",

  async join(ctx) {
    ctx.body = await Service.service.user.join(ctx.request.body);
  },

  async login(ctx) {
    const res = await Service.service.user.login(ctx.request.body);
    if (res.ok) {
      const token = jwt.sign({ u_id: res.id }, USER_TOKEN_SECRET_KEY);
      ctx.set(TOKEN_ISSUE_KEY, token);
    }
    ctx.body = res;
  },

  async del(ctx) {
    ctx.body = await Service.service.user.del(ctx.request.body);
  },
};
